package com.charmzenus.message.redis2;

import com.charmzenus.message.MessageApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@Import( value = MessageApplication.class )
public class PublisherConfiguration {
 
    @Bean
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter( new RedisMessageListener() );
    }

    @Autowired
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory ) {
 
        final RedisMessageListenerContainer container =
                new RedisMessageListenerContainer();
        container.setConnectionFactory( connectionFactory );
        container.addMessageListener( messageListener(), new ChannelTopic( "__keyevent@0__:expired" ) );
 
        return container;
    }

}
